package controller

import (
	"bluebell/logic"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

// CommunityHandler
// @Tags CommunityApi
// @Summary 查询到所有的社区(community_id,community_name)以列表的形式返回
// @Schemes
// @Description	Get 请求
// @Accept json
// @Produce json
// @Success 200 {string} json{"code","message","data"}
// @Router	/api/community/community [get]
func CommunityHandler(c *gin.Context) {
	communityList, err := logic.GetCommunityList()
	if err != nil {
		zap.L().Error("logic.GetCommunityList() failed", zap.Error(err))
		ResponseError(c, CodeServerBusy) // 不轻易把服务端报错暴露给外面
		return
	}
	ResponseSuccess(c, communityList)
}

// CommunityDetailHandler
// @Tags CommunityApi
// @Summary 根据ID查找到社区分类的详情
// @Schemes
// @Description	Get 请求, 参数在url中
// @Accept json
// @Produce json
// @Param	id query int true "社区id"
// @Success 200 {string} json{"code","message","data"}
// @Router	/api/post/community [get]
func CommunityDetailHandler(c *gin.Context)  {
	// 1、获取社区ID
	communityIdStr := c.Param("id")  // 获取URL参数
	communityId, err := strconv.ParseUint(communityIdStr, 10, 64) // id字符串格式转换
	if err != nil {
		ResponseError(c, CodeInvalidParams)
		return
	}
	// 2、根据ID获取社区详情
	communityList, err := logic.GetCommunityDetailByID(communityId)
	if err != nil {
		zap.L().Error("logic.GetCommunityByID() failed", zap.Error(err))
		ResponseErrorWithMsg(c, CodeServerBusy, err.Error())
		return
	}
	ResponseSuccess(c, communityList)
}